package com.harunasan.carrental.api.serve.controller;


import com.harunasan.carrental.api.serve.domain.entity.CmsCar;
import com.harunasan.carrental.api.serve.domain.post.CmsCarPost;
import com.harunasan.carrental.api.serve.service.ICmsCarService;
import com.harunasan.carrental.common.api.CommonPage;
import com.harunasan.carrental.common.api.CommonResult;
import com.harunasan.carrental.common.query.CommonQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-02 23:26:10
 */
@RestController
@Api(tags = "车辆管理")
@RequestMapping("/api/serve/car")
public class CmsCarController {
    @Autowired
    private ICmsCarService cmsCarService;

    @PostMapping()
    @ApiOperation("添加车辆")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> carCreate(@RequestBody CmsCarPost car) {
        return CommonResult.success(cmsCarService.create(car));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除车辆信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> carRemove(@ApiParam(name = "id", value = "车辆标识", required = true)
                                           @PathVariable Long id) {
        return CommonResult.success(cmsCarService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改车辆信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> carUpdate(
            @ApiParam(name = "id", value = "车辆标识", required = true)
            @PathVariable Long id, @RequestBody CmsCarPost car) {
        return CommonResult.success(cmsCarService.update(id, car));
    }

    @GetMapping()
    @ApiOperation("获取车辆列表")
    public CommonResult<List<CmsCar>> carList(CommonQueryParam commonQueryParam) {
        return CommonResult.success(cmsCarService.list(commonQueryParam));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取车辆详情")
    public CommonResult<CmsCar> carObj(@PathVariable Long id) {
        return CommonResult.success(cmsCarService.detail(id));
    }

    @GetMapping("/page")
    @ApiOperation("获取车辆分页")
    public CommonResult<CommonPage<CmsCar>> carPage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(cmsCarService.pageList(commonQueryParam)));
    }

}
